/*
 ******************************************************************************
 *
 *       Copyright ASTRI, 2016.  All rights reserved.
 *
 *   This software is proprietary to and embodies the confidential technology
 *   of Hong Kong Applied Science and Technology Research Institute Company
 *   Limited (ASTRI).
 *   Possession, use, or copying of this software and media is authorized
 *   only pursuant to a valid written license from ASTRI or an authorized
 *   sublicensor.
 *
 * File Name:
 *   XXX.java
 *
 * Purpose:
 *   
 *   
 *   
 *
 * Project or Modules Name:
 *   
 *
 * Modules Contained:
 *   N/A
 *
 * Modification History:
 * Date(DDMMYY)  Version No Modified By  Description
 * ------------  ---------- -----------  -----------
 * 20160405       0.0.0.1    XXX     Created
 *
 ******************************************************************************
 */


package com.j.thetabk.thetas;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.j.thetabk.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;

/**
 * Created by user on 20/4/2016.
 */
public class ThetaSDoc {

    public String sDir="/mnt/sdcard/thetaSbracket";
    public String settingExtension = ".txtb";
    public String settingName="default";
    public String seg="\r\n";

//    public ThetaSDoc()
//    {
//        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
//
//        sDir = sharedPref.getString("sDir", "/mnt/sdcard/thetaSbracket");
//    }


    public void LoadDir(SharedPreferences sharedPref)
    {
        sDir = sharedPref.getString("sDir", "/mnt/sdcard/thetaSbracket");
    }

    public void SaveDir(SharedPreferences sharedPref)
    {
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("sDir", sDir);
        editor.commit();
    }


    public class SetFileNameFilter implements FilenameFilter
    {
        @Override
        public boolean accept(File dir, String filename) {
            if(filename.endsWith(settingExtension))
                return true;
            return false;
        }
    }

    public File[] GetFileList()
    {
        File destDir = new File(sDir);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }

        File[] files=destDir.listFiles(new SetFileNameFilter());
        return files;
    }

    public void SaveFileListSD(String fileName,  String [] fileList) throws IOException
    {
        File destDir = new File(sDir);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        File file = new File(sDir+"/"+fileName);
        FileOutputStream fos = new FileOutputStream(file);
        for(String tmp:fileList)
        {
            fos.write((tmp+seg).getBytes());
        }
        fos.close();
    }


    public interface PropertyChangeListener {
        void onNameChanged();
        void onFolderChanged();
        void onFileListEmpty();
        void onSelectFile(File file);
    }

    public PropertyChangeListener mListener=null;


    public void EditSettingName(Context ctx)
    {
        View dialoglayout= LayoutInflater.from(ctx).inflate(R.layout.editlayout, null);

        final EditText edittext = (EditText)dialoglayout.findViewById(R.id.edtFileName);
        edittext.setText(settingName);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
        alertDialogBuilder.setTitle(R.string.save_settings);
        alertDialogBuilder.setView(dialoglayout);
        alertDialogBuilder.setPositiveButton(R.string.ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int id) {
                        settingName = edittext.getText().toString();
                       if(mListener!=null)
                           mListener.onNameChanged();
                    }
                });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

    public void LoadSettingList(Context ctx)
    {
        final File[] files=GetFileList();

        if(files.length<1)
        {
            if(mListener!=null)
                mListener.onFileListEmpty();
            return;
        }

        String [] fnl=new String[files.length];
        for(int i=0;i<files.length;i++)
            fnl[i]=files[i].getName();

        new AlertDialog.Builder(ctx, AlertDialog.THEME_HOLO_DARK)
                .setTitle(R.string.load_settings)
                .setItems(fnl, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(mListener!=null)
                            mListener.onSelectFile(files[which]);
                    }
                })
                .show();
    }

}
